/*!
 *  @author  xiaody <xiaody09@163.com>
 *  @description  Editor enhancement module.
 */

(function(){

'use strict';

var EditorPlus = {

	init : function(){
		var editor = $('#editor');
		if(!editor)
			return false;

		var toolbar = $(".tb-editor-toolbar"), doc = document;
		if(toolbar){
			var miyu_span = doc.createElement('span').attr({'class':'miyutool','data-cmd':'miyutool','title':'密语'});
			miyu_span.addEventListener('click', EditorPlus.encodePost, false);

			var yycode_span = doc.createElement('span').attr({'class':'yycodetool','data-cmd':'yycodetool','title':'UUcode'});
			yycode_span.addEventListener('click', UUcode.codeInsert, false);

			var sw = doc.createElement('span').attr({
				id : 'editortoolswitch',
				title : '奇怪的工具们',
				onclick : '$(".tb-editor-toolbar>*:not([id])").animate({width:"toggle",paddingLeft:"toggle",opacity:"toggle"},300)'
			});
			sw.$text('##');

			miyu_span.$text('密语');
			yycode_span.$text('UUcode');

			toolbar.pushChild(sw).pushChild(miyu_span).appendChild(yycode_span);
		}

		$('form', editor).addEventListener('focus', initUnicode, true);

		inject(injectFuc);
	},
	encodePost : function(){
		var editarea = $(".tb-editor-editarea");
		var pos = editarea.html();

		if(!pos) return;
		if(pos.substring(0,8) !== '#Base64#' && pos.substring(0,4) !== '百度密语') {
			sessionStorage.youopost = pos;
			editarea.innerHTML = Miyu.encode(sessionStorage.youopost);
			editarea.attr('contenteditable', false);
		} else if(pos.substring(0,4) === '百度密语') {
			editarea.innerHTML = '#Base64#' + Base64.encode(sessionStorage.youopost);
			editarea.attr('contenteditable', false);
		} else if(pos.substring(0,8) === '#Base64#') {
			editarea.innerHTML = sessionStorage.youopost;
			editarea.attr('contenteditable', true);
		}
	}

};

function initUnicode(){
	if($('#useUnicode'))
		return;

	var mainTd = $('#editor .editor_users');
	if(!mainTd)
		return false;
	else
		mainTd = mainTd.parentNode;

	var div = document.createElement('div');
	div.html('<input type=checkbox id=useUnicode><label for=useUnicode title="繁体字、反和谐时用">使用Unicode发表</label>');
	mainTd.appendChild(div);

	$('#editor form').removeEventListener(initUnicode);
}

var injectFuc = function(){
		function u(str){
			var f=[],str=str.replace(/<br.*?>/g, '<br>');
			
			// This line mean to keep links in blue,
			// but I'm afraid it's really fragile.
			//str=str.replace(/<a ([^> ]* )??href=['"][^>]*?>/g,  '<a href="https://\'" target="_blank">');
			
			for(var r="",i=0,l=str.length;i<l;i++){
				if(/[<&]/.test(str[i])&&f[f.length-1]==='@'){f.pop()}
				if(/[<@&]/.test(str[i])){f.push(str[i])}
				if(f.length===0){r+="&#"+str.charCodeAt(i)+";"}
				else{r+=str[i]}
				if(str[i]==='>'&&f[f.length-1]==='<'){f.pop()}
				else if(str[i]===';'&&f[f.length-1]==='&'){f.pop()}
			}
			return r;
		}
		RichPostor.prototype._getData = function(){
			var C = this._data_postor, E = this._data_editor, B = this._dom_id;
			if(this._option.title){
				var I = document.getElementById(this._option.title);
				if(I){
					C.title = $("#" + this._option.title).val();
				}
			}
			var D = E.is_login,
				H = document.getElementById("useSignName"),
				A = document.getElementById(B.post_login),
				F = document.getElementById("sign_id"),
				U = document.getElementById("useUnicode");
				
			// If you delete the links-keep-blue trick upper,
			// you need to un-comment this block, too.
			if(U && U.checked)
				TED.Editor.prototype.parseURL = function(K){};

			if (D && H && H.checked && F)
				C.sign_id = F.value;
			if (D && !A.checked)
				C.anonymous = 1;
			C.tbs = this._option.tbs;
			var G = this._captcha.getInputValue();
			if (G != "")
				C.vcode = G;
			C.vcode_md5 = this._captcha._option.sign_str;
			C.content = (U && U.checked) ? u(this._editor.getHtml()) : this._editor.getHtml();
			return C;
		};
		
		var config = TED.Editor.getInstanceById('tb-editor-1').getConfig();
		config.bavl = 18, config.picasso = true;
		
		PageData.user.is_login = PageData.user.is_login || 1;
};

window.EditorPlus = EditorPlus;


})();